from Settings import Settings
from pimDiffer import pimDiffer
from pimRestorer import pimRestorer
from pimWriter import pimWriter
from sqliteDB import sqliteDB
time=__import__('time')
import os;


def vsync():
    
    settings=Settings('/etc/vsync/settings.xml')
    settings.parseSettings();

    if os.path.getsize(settings.svnWorkingCopy+'/index/'+settings.fts_db)==0:
        #database parameters
        fts_db=sqliteDB(settings.svnWorkingCopy+'/index/'+settings.fts_db)
        fts_table_names=["contacts_index","calendar_index","memos_index"]
        modified_db_table_names=["modified_contacts","modified_calendar","modified_memos"]
        removed_db_table_names=["removed_contacts","removed_calendar","removed_memos"]
        
        #database creation: tables -->index,modified entries,removed_entries
        for table in fts_table_names:
            fts_db.open().execute('CREATE VIRTUAL TABLE '+table+' USING fts3(uid VARCHAR,rev INTEGER,state,body,cutted,timestamp,removed_timestamp)')
            fts_db.commit();
            fts_db.close()
        for table in modified_db_table_names:
            fts_db.open().execute('CREATE TABLE '+table+' (uid VARCHAR PRIMARY KEY,revision INTEGER,timestamp VARCHAR)')
            fts_db.commit();
            fts_db.close()
        for table in removed_db_table_names:
            fts_db.open().execute('CREATE TABLE '+table+' (uid VARCHAR PRIMARY KEY,revision INTEGER,timestamp VARCHAR,removed_timestamp VARCHAR)')
            fts_db.commit();
            fts_db.close()
            
        

    #start pimdiffer thread
    #pimdiffer=pimDiffer(settings);
    #pimdiffer.start();
    #pimrestore= pimRestorer(settings)
    #pimrestore.restore("20110130T221216Z-347-1000-1-0@alessio-laptop", 39, 1)
    
if __name__ == '__main__':
    vsync()
    